<template>
  <div class="app-container">
    <el-dialog title="期数日历" :visible.sync="calendarVisible" width="1200px" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-calendar v-model="myMonth">
        <!-- 这里使用的是 2.5 slot 语法，对于新项目请使用 2.6 slot 语法-->
        <template
          slot="dateCell"
          slot-scope="{date, data}"
        >
          <div v-if="showVolName(data.day)" style="height:70px;" @click="addVol(showVolData(data.day))">
            <div style="font-weight:bolder;color:blue;line-height:150%" v-html="data.day+'<br>第'+formatYear(showVolName(data.day))+'期'" />
            <!-- <div v-if="selectVols.indexOf(showVolName(data.day))!==-1">✔️</div> -->
          </div>
          <div v-else>{{ data.day }}</div>
        </template>
      </el-calendar>
    </el-dialog>
    <el-dialog title="信息详情" :visible.sync="previewVisible" top="10px" width="1200px" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form ref="postForm" :model="postForm" :rules="rules" class="form-container" label-width="200px" :inline="true">
        <table width="100%" border="0" cellpadding="15" cellspacing="1" class="inputtable">
          <tr>
            <td width="120" align="center" valign="top" bgcolor="#F9FBFB"><strong>信息类别</strong></td>
            <td valign="top" bgcolor="#FFFFFF">
              <el-form-item v-if="isEdit" label="" prop="infoClass">
                <el-select v-model="postForm.infoClass" placeholder="请选择" @change="changeInfoClass">
                  <el-option-group
                    v-for="group in infoClassList"
                    :key="group.id"
                    :label="group.classname"
                  >
                    <el-option
                      v-for="item1 in group.children"
                      :key="item1.id"
                      :label="item1.classname"
                      :value="item1.id"
                    />
                  </el-option-group>
                </el-select>
              </el-form-item>
              <span v-else>{{ infoClassName(detail.classId) }}</span>
            </td>
          </tr>
          <tr>
            <td align="center" valign="top" bgcolor="#F9FBFB"><strong>内容</strong></td>
            <td valign="top" bgcolor="#FFFFFF" style="word-wrap:break-word; word-spacing:normal;">
              <el-form-item v-if="isEdit" label="" prop="title">
                <el-input v-model="postForm.title" style="width:800px;clear:both" @input="changeTextNum" />
              </el-form-item>
              <el-form-item v-if="isEdit" label="" prop="content">
                <el-input v-model="postForm.content" style="width:800px;clear:both;margin-top:10px" rows="3" type="textarea" @input="changeTextNum" />
              </el-form-item>
              <div v-if="isEdit">
                <el-form-item label="" prop="phone">
                  电话1： <el-input v-model="postForm.phone" style="width:200px;clear:both;margin-top:10px" @input="changeTextNum" />
                </el-form-item>
                <el-form-item label="" prop="phone2">
                  电话2： <el-input v-model="postForm.phone2" style="width:200px;clear:both;margin-top:10px" @input="changeTextNum" />
                </el-form-item>
                <div>字数：{{ postForm.textnum }}<span v-if="postForm.textnum>limitTextNum">（超{{ postForm.textnum-limitTextNum }}字）</span></div>
              </div>
              <div v-else id="pre_content" style="word-wrap:break-word; word-spacing:normal;">
                <div>{{ detail.alltext }}</div>
                <div>字数：{{ detail.textnum }}<span v-if="detail.textnum>limitTextNum">（超{{ detail.textnum-limitTextNum }}字）</span></div>
              </div>
            </td>
          </tr>
          <tr v-if="isShowPaper">
            <td align="center" valign="top" bgcolor="#F9FBFB"><strong>刊登期数</strong></td>
            <td v-if="!isEdit" valign="top" bgcolor="#FFFFFF" style="word-wrap:break-word; word-spacing:normal;">
              <div id="pre_vol" style="word-wrap:break-word; word-spacing:normal;">{{ detail.vols }}</div>
              <div>共 {{ detail.volnum }} 期
                <span v-if="!detail.week1||!detail.week3||!detail.week5"> 只登 <span v-if="detail.week1">周一</span> <span v-if="detail.week3">周三</span> <span v-if="detail.week5">周五</span> </span>
              </div>
            </td>
            <td v-else valign="top" bgcolor="#FFFFFF">
              <el-form-item label="" prop="volnum">
                从第<el-input-number v-model="postForm.startVol" style="width:170px;" />期开始，刊登
                <el-input-number v-model="postForm.volnum" :min="0" :max="350" style="width:130px;" />期
                <el-button type="success" @click="setVol">计算</el-button>
                <div>
                  <el-checkbox v-model="postForm.week1" label="周一" />
                  <el-checkbox v-model="postForm.week3" label="周三" />
                  <el-checkbox v-model="postForm.week5" label="周五" />
                </div>
                <div>
                  <el-input v-model="postForm.vols" type="textarea" style="width:502px;" rows="3" @blur="checkVols()" />
                </div>
                <div>
                  <el-button type="success" icon="el-icon-success" @click="checkVols()">验证</el-button>
                  <el-button type="primary" icon="el-icon-date" @click="showCalendar">期数日历</el-button>
                </div>
              </el-form-item>
            </td>
          </tr>
          <tr>
            <td align="center" valign="top" bgcolor="#F9FBFB"><strong>微信小程序</strong></td>
            <td v-if="!isEdit" valign="top" bgcolor="#FFFFFF" style="word-wrap:break-word; word-spacing:normal;">
              <!-- <div v-if="detail.weixin">同步刊登至微信小程序 <span v-if="detail.wxTopDays">置顶 {{ detail.wxTopDays }} 天</span></div> -->
              <div>
                刊登 {{ detail.wxNum || 0 }} 天
                <span v-if="detail.wxWeeksText">（只登{{ detail.wxWeeksText }}）</span>
              </div>
              <div>
                置顶 {{ detail.wxTopDays || 0 }} 天
              </div>
            </td>
            <td v-else style="vertical-align: top" bgcolor="#FFFFFF">
              <!-- <el-form-item label="" prop="weixin">
                <el-checkbox v-model="postForm.weixin" style="float:left" label="同步刊登至微信小程序" /><br>
                <div v-if="postForm.weixin">置顶 <el-input-number v-model="postForm.wxTopDays" :min="0" :max="1000" style="width:130px;" /> 天</div>
              </el-form-item> -->
              <div>刊登 <el-input-number v-model="postForm.wxNum" :min="0" :max="1000" style="width:130px;" /> 天</div>
              <div>
                <el-checkbox v-model="checkAll" :indeterminate="isIndeterminate" @change="handleCheckAllChange">全选</el-checkbox>
                <el-checkbox-group v-model="checkedWeek" @change="handleCheckedWeeksChange">
                  <el-checkbox v-for="week in weekOptions" :key="week.value" :label="week.value">{{ week.label }}</el-checkbox>
                </el-checkbox-group>
              </div>
              <div style="border-top: 1px solid #DCDFE6; margin-bottom: 14px;" />
              <div>置顶 <el-input-number v-model="postForm.wxTopDays" :min="0" :max="1000" style="width:130px;" /> 天</div>
            </td>
          </tr>
          <tr v-if="detail.wxUser">
            <td align="center" valign="top" bgcolor="#F9FBFB"><strong>小程序用户</strong></td>
            <td style="vertical-align: top" bgcolor="#FFFFFF">
              <el-image
                style="width: 50px; height: 50px;margin-right:10px;"
                :src="detail.wxUser.avatarUrl"
                fit="fill"
                :preview-src-list="[detail.wxUser.avatarUrl]"
              />
              <div>{{ detail.wxUser.nickname }}</div>
            </td>
          </tr>
          <tr v-if="detail.files&&detail.files.length>0">
            <td align="center" valign="top" bgcolor="#F9FBFB"><strong>图片</strong></td>
            <td valign="top" bgcolor="#FFFFFF">
              <el-form-item label="" prop="pics">
                <div
                  v-for="it in detail.files"
                  :key="it"
                  style="float:left"
                >
                  <el-image
                    style="width: 100px; height: 100px;margin-right:10px;"
                    :src="$http.defaults.baseURL + 'api/mpfile/download?id=' + it.id"
                    fit="fill"
                    :preview-src-list="detail.files.map(file => $http.defaults.baseURL + 'api/mpfile/download?id=' + file.id)"
                  /><br>
                  <el-popconfirm
                    v-if="isEdit"
                    title="此操作无需保存，立即删除该图片信息，确定删除吗？"
                    @onConfirm="delPic(it.id)"
                  >
                    <el-button slot="reference" type="text" style="text-align:center;margin-left:33px;">删除</el-button>
                  </el-popconfirm>
                </div>
              </el-form-item>
            </td>
          </tr>
          <tr v-if="isShowPaper">
            <td align="center" valign="top" bgcolor="#F9FBFB"><strong>加黑提前</strong></td>
            <td valign="top" bgcolor="#FFFFFF">
              <el-form-item v-if="isEdit" label="" prop="isBold">
                <template v-if="postForm.infoClass===70 && paperFlag!=='1'">
                  <el-radio-group v-model="postForm.textb" @change="setTextB">
                    <el-radio style="float:left" :label="2">标下</el-radio>
                    <el-radio style="float:left" :label="3">标头</el-radio>
                  </el-radio-group>
                </template>
                <template v-else>
                  <el-checkbox v-model="postForm.isBlack" style="float:left" label="加黑" @change="setTextB" />
                  <el-checkbox v-model="postForm.isBold" style="float:left" label="提前" @change="setTextB" />
                </template>
              </el-form-item>
              <span v-else>{{ showTextB(detail.textb) }}</span></td>
          </tr>
          <tr v-if="detail.wxUser&&detail.status">
            <td align="center" valign="top" bgcolor="#F9FBFB"><strong>金额</strong></td>
            <td bgcolor="#FFFFFF">{{ detail.price }}.00元</td>
          </tr>
          <tr v-if="detail.wxUser&&detail.status">
            <td align="center" valign="top" bgcolor="#F9FBFB"><strong>商户订单号</strong></td>
            <td bgcolor="#FFFFFF">{{ detail.outTradeNo }}</td>
          </tr>
          <tr v-if="detail.wxUser&&detail.status">
            <td align="center" valign="top" bgcolor="#F9FBFB"><strong>微信支付单号</strong></td>
            <td bgcolor="#FFFFFF">{{ detail.transactionId }}</td>
          </tr>
          <!-- <tr>
          <td align="center" valign="top" bgcolor="#F9FBFB"><strong>备注</strong></td>
          <td valign="top" bgcolor="#FFFFFF">{{ detail.remark }}</td>
        </tr>
        <tr>
          <td align="center" valign="top" bgcolor="#F9FBFB"><strong>提示信息</strong></td>
          <td valign="top" bgcolor="#FFFFFF"><span id="pre_message" />&nbsp;<span v-if="detail.textnum>limitTextNum">超{{ detail.textnum - limitTextNum }}字</span>&nbsp;共{{ detail.price }}元<span v-if="detail.memberCard">（使用会员卡）</span></td>
        </tr> -->
          <tr v-if="!isEdit&&source===2">
            <td align="center" valign="top" bgcolor="#F9FBFB"><strong>未通过原因</strong></td>
            <td valign="top" bgcolor="#FFFFFF" style="word-wrap:break-word; word-spacing:normal;">
              <el-form-item label="" prop="delermarks">
                <el-input v-model="postForm.delermarks" style="width:800px;clear:both" />
              </el-form-item>
            </td>
          </tr>
          <tr>
            <td align="center" valign="top" bgcolor="#F9FBFB" />
            <td valign="top" bgcolor="#FFFFFF">
              <el-button v-if="!isEdit" type="success" :loading="loading" @click="passInfo(detail.id)">通过</el-button>
              <el-button v-if="!isEdit&&source===2" type="danger" :loading="loading" @click="noPassInfo(detail.id)">未通过</el-button>
              <el-button v-if="!isEdit" type="warning" @click="editInfo(true)">修改</el-button>
              <el-button v-if="isEdit" type="success" :loading="loading" @click="editSubmit()">提交</el-button>
              <el-button v-if="isEdit" @click="editInfo(false)">取消</el-button>
            </td>
          </tr>
        </table>
      </el-form>
    </el-dialog>

    <div v-if="!showList" class="filter-container">
      <div style="float:left;">
        <!-- <el-radio-group v-model="listQuery.status" class="filter-item">
          <el-radio-button :label="1">一校</el-radio-button>
          <el-radio-button :label="2">二校</el-radio-button>
        </el-radio-group> -->
        <el-input v-model="listQuery.vol" placeholder="期数" style="margin-left: 10px;width: 100px;" class="filter-item" @input="listQuery.vol=listQuery.vol.replace(/^(0+)|[^\d]+/g,'')" />
        <el-button class="filter-item" style="margin-left: 10px;" icon="el-icon-date" @click="showCalendar">
          期数日历
        </el-button>
        <el-button class="filter-item" type="primary" icon="el-icon-search" :loading="btnLoading" style="margin-left: 10px;" @click="handleFilter">
          确定
        </el-button>
        <el-button class="filter-item" type="success" icon="el-icon-chat-dot-round" :loading="btnLoading" style="margin-left: 10px;" @click="handleWxFilter">
          校对只刊登微信小程序信息
        </el-button>
      </div>
    </div>
    <template v-else>
      <el-radio-group v-model="source" style="vertical-align:top;margin-bottom:10px" @change="handleFilter">
        <el-radio-button :label="1">前台</el-radio-button>
        <el-radio-button :label="2">微信小程序</el-radio-button>
      </el-radio-group>
      <el-select v-model="listQuery.infoClass" placeholder="请选择" style="margin-left: 10px;">
        <el-option
          label="全部"
          :value="null"
        />
        <el-option
          v-for="group in infoClassList"
          :key="group.id"
          :value="group.id"
          :label="group.classname"
        />
      </el-select>
      <el-input v-model="listQuery.realname" placeholder="姓名" style="width: 100px;margin-left: 10px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input ref="phoneInput" v-model="listQuery.phone" placeholder="电话" style="width: 150px;margin-left: 10px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.id" placeholder="编号" style="width: 100px;margin-left: 10px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.key" placeholder="关键词" style="width: 150px;margin-left: 10px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" style="margin-left: 10px;" @click="handleFilter" @keyup.enter.native="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" icon="el-icon-back" style="float:right" @click="backSelect">
        返回
      </el-button>
      <el-table
        ref="multipleTable"
        v-loading="listLoading"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%"
        @sort-change="sortChange"
      >
        <!-- <el-table-column v-if="roleEdit" type="selection" width="50" align="center" /> -->
        <el-table-column align="center" prop="id" label="信息编号" width="120px" />
        <el-table-column align="center" prop="realname" label="姓名" width="120px" />
        <el-table-column align="center" prop="publishUserId" label="录入员" width="120px">
          <template slot-scope="scope">
            {{ scope.row.publishUserId? scope.row.publishUserId:'个人' }}
          </template>
        </el-table-column>
        <el-table-column width="250px" prop="classId" label="信息分类">
          <template slot-scope="scope">
            {{ infoClassName(scope.row.classId) }}
          </template>
        </el-table-column>

        <el-table-column width="200px" prop="adddate" align="center" label="录入时间" />
        <el-table-column width="150px" prop="status" align="center" label="状态">
          <template slot-scope="scope">
            {{ statusList.find(item=>item.value===scope.row.status).label }}
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="300">
          <template slot-scope="scope">
            <el-button size="small" type="primary" icon="el-icon-search" @click="detailShow(scope.row.id)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :page-sizes="[20,50,100,200]"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
    </template>
  </div>
</template>

<script>
import qs from 'qs'
// import { fetchList } from '@/api/article'
import Pagination from '@/components/Pagination'

const defaultForm = {
  delermarks: null,
  memberCard: false,
  duration: 0,
  startDate: [],
  infoClass: '',
  realname: '',
  phone: '',
  phone2: '',
  idcard: '',
  volnum: '',
  isBold: '',
  isBlack: '',
  textb: '',
  content: '',
  title: '',
  // pics: [
  //   { src: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg', url: ['https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg'] },
  //   { src: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg', url: ['https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg'] },
  //   { src: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg', url: ['https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg'] }
  // ],
  price: '',
  // orderId: '',
  // dataCreateDatetime: '',
  // passedList: [],
  remark: '',
  vols: '',
  id: null,
  weixin: false,
  wxTopDays: 0,
  startVol: null,
  week1: false,
  week3: false,
  week5: false,
  wxNum: 0
}
export default {
  name: 'ArticleList',
  components: { Pagination },
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value === null || value === undefined || value === '') {
        // this.$message({
        //   message: '请将表单填写完整',
        //   type: 'error'
        // })
        callback(new Error('必填项'))
      } else {
        callback()
      }
    }
    return {
      source: 1,
      alltext: '',
      rules: {
        infoClass: [{ validator: validateRequire }],
        title: [{ validator: validateRequire }],
        content: [{ validator: validateRequire }],
        phone: [{ validator: validateRequire }]
      },
      postForm: Object.assign({}, defaultForm),
      isEdit: false,
      monthVolList: [],
      selectVols: [],
      myMonth: null,
      calendarVisible: false,
      showList: false,
      limitTextNum: 65,
      loading: false,
      detail: {
        id: null,
        classId: null,
        realname: '',
        idcard: '',
        alltext: '',
        textNum: 0,
        textb: 0,
        vols: '',
        volnum: 0,
        remark: '',
        weixin: false,
        wxTopDays: 0,
        startVol: null,
        week1: false,
        week3: false,
        week5: false
      },
      previewVisible: false,
      statusList: [{ value: 0, label: '未审核' }, { value: 1, label: '未校对' }, { value: 2, label: '一校通过' }, { value: 3, label: '已校对' }, { value: 4, label: '已拒绝' }, { value: 8, label: '已删除' }],
      infoClassList: [],
      unitName: '',
      roleName: '',
      cfajbh: '',
      showStep: [],
      showAddBtn: false,
      roleEdit: true,
      selection: false,
      dicListLoading: false,
      stopDicLoading: false,
      startDicLoading: false,
      submitLoading: false,
      classList: [],
      classForm: {
        parentClass: 14,
        label: ''
      },
      diclist: [],
      settingVisible: false,
      item: {},
      dialogtitle: '',
      dialogVisible: false,
      allDialogVisible: false,
      readOnly: false,
      timestamp: '',
      list: [
      ],
      list2: [],
      list3: [],
      total: 0,
      listLoading: true,
      listQuery: {
        realname: '',
        phone: '',
        id: null,
        key: '',
        status: 1,
        vol: '',
        infoClass: null,
        page: 1,
        limit: 100
      },
      Sortkey: '',
      Isace: '',
      multipleSelection: [],
      id: '',
      step: '',
      showThansferBtn: false,
      knowledgeVisible: false,
      checkAll: false,
      weekOptions: [
        { label: '周一', value: 1 },
        { label: '周二', value: 2 },
        { label: '周三', value: 3 },
        { label: '周四', value: 4 },
        { label: '周五', value: 5 },
        { label: '周六', value: 6 },
        { label: '周日', value: 7 }
      ],
      isIndeterminate: false,
      checkedWeek: [],
      btnLoading: false,
      wxVolname: null,
      paperFlag: null,
      isShowPaper: true
    }
  },
  watch: {
    myMonth: {
      handler(val) {
        this.getVolsByDate()
      },
      immediate: true
    }
  },
  mounted() {
    this.getInfoClass()
    this.getDefaultVol()
    this.paperFlag = localStorage.getItem('paperFlag')
  },
  created() {
    // this.getList()
    window.addEventListener('keydown', this.handkeyCode, true)// 开启监听键盘按下事件
  },
  methods: {
    // 周几全选
    handleCheckAllChange(val) {
      this.checkedWeek = val ? this.weekOptions.map(item => item.value) : []
      this.isIndeterminate = false
    },
    handleCheckedWeeksChange(value) {
      const checkedCount = value.length
      this.checkAll = checkedCount === this.weekOptions.length
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.weekOptions.length
    },
    handkeyCode(e) {
      let key = null
      if (window.event === undefined) {
        key = e.keyCode
      } else {
        key = window.event.keyCode
      }
      if (key === 13) {
        this.handleFilter()
      }
    },
    backSelect() {
      this.showList = false
      this.listQuery.realname = ''
      this.listQuery.phone = ''
      this.listQuery.id = null
      this.listQuery.key = ''
      // this.listQuery.status = null
    },
    delPic(id) {
      this.$http
        .get('/api/genFileInfo/remove?id=' + id)
        .then(response => {
          if (response.data.success === true) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.detail.files = this.detail.files.filter(item => item.id !== id)
          } else {
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
        })
    },
    setVol() {
      this.postForm.vols = ''
      if (this.postForm.volnum > 0) {
        this.$http
        // .get('/api/busVol/getVolList?startVol='+this.postForm.startVol+'&num='+this.postForm.volnum+,)
          .get('/api/busVol/getVolList?' + qs.stringify({ startVol: this.postForm.startVol, num: this.postForm.volnum, week1: this.postForm.week1, week3: this.postForm.week3, week5: this.postForm.week5 }))
          .then(response => {
            if (response.data.success === true) {
              const vols = response.data.result.map(item => {
                const year = new Date(item.pubdate).getFullYear()
                const nowYear = new Date().getFullYear()
                if (year !== nowYear) {
                  return (year - 2000) * 1000 + parseInt(item.name)
                } else {
                  return item.name
                }
              })
              this.checkVols(vols)
            } else {
              this.postForm.vols = ''
              this.$message({
                message: '获取失败:' + response.data.message,
                type: 'error'
              })
            }
          })
          .catch(error => {
            console.log(error)
          })
      }
    },
    editSubmit() {
      const para = {
        infoId: this.postForm.id,
        classId: this.postForm.infoClass,
        status: 3,
        title: this.postForm.title,
        content: this.postForm.content,
        phone: this.postForm.phone,
        phone2: this.postForm.phone2,
        textnum: this.postForm.textnum,
        textb: this.postForm.textb,
        volList: this.postForm.vols ? this.postForm.vols.split(',') : [],
        volnum: this.postForm.vols ? this.postForm.vols.split(',').length : 0,
        weixin: this.postForm.wxNum > 0 || this.postForm.wxTopDays > 0 ? 1 : 0,
        wxTopDays: this.postForm.wxTopDays,
        wxNum: this.postForm.wxNum,
        wxWeek1: this.checkedWeek.includes(1) ? 1 : 0,
        wxWeek2: this.checkedWeek.includes(2) ? 1 : 0,
        wxWeek3: this.checkedWeek.includes(3) ? 1 : 0,
        wxWeek4: this.checkedWeek.includes(4) ? 1 : 0,
        wxWeek5: this.checkedWeek.includes(5) ? 1 : 0,
        wxWeek6: this.checkedWeek.includes(6) ? 1 : 0,
        wxWeek7: this.checkedWeek.includes(7) ? 1 : 0
      }
      if (para.wxNum > 0 && this.checkedWeek.length === 0) {
        this.$message({
          message: '微信周几显示至少选择一天',
          type: 'error'
        })
        return
      }
      this.loading = true
      this.$http
        .post('/api/busPassInfo/save', para)
        .then(response => {
          this.loading = false
          if (response.data.success === true) {
            this.isEdit = false
            this.$message({
              message: '校对成功',
              type: 'success'
            })
            this.previewVisible = false
            this.getList()
          } else {
            console.log('校对失败:' + response.data.message)
            this.$message({
              message: '校对失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
          this.loading = false
        })
    },
    setTextB() {
      if (this.postForm.infoClass !== 70) {
        if (this.postForm.isBlack) {
          if (this.postForm.isBold) {
            this.postForm.textb = 3
          } else {
            this.postForm.textb = 1
          }
        } else {
          if (this.postForm.isBold) {
            this.postForm.textb = 2
          } else {
            this.postForm.textb = 0
          }
        }
      }
      if (this.postForm.infoClass === 70) {
        switch (this.postForm.textb) {
          case 2:
            this.textbPrice = 80
            break
          case 3:
            this.textbPrice = 100
            break
        }
      } else {
        switch (this.postForm.textb) {
          case 0:
            this.textbPrice = 0
            break
          case 1:
            this.textbPrice = 25
            break
          case 2:
            this.textbPrice = 30
            break
          case 3:
            this.textbPrice = 55
            break
        }
      }
    },
    changeTextB() {
      if (this.postForm.infoClass !== 70) {
        switch (this.postForm.textb) {
          case 0:
            this.postForm.isBlack = false
            this.postForm.isBold = false
            break
          case 1:
            this.postForm.isBlack = true
            this.postForm.isBold = false
            break
          case 2:
            this.postForm.isBlack = false
            this.postForm.isBold = true
            break
          case 3:
            this.postForm.isBlack = true
            this.postForm.isBold = true
            break
        }
      }
    },
    changeInfoClass() {
      if (this.postForm.infoClass === 70) {
        this.postForm.textb = 2
      } else {
        this.postForm.textb = 0
      }
      this.setTextB()
    },
    changeTextNum() {
      let alltext = this.postForm.title + ':' + this.postForm.content
      if (this.postForm.phone) {
        alltext = alltext + '。电话：' + this.postForm.phone
      }
      if (this.postForm.phone2) {
        alltext = alltext + ' ' + this.postForm.phone2
      }
      this.alltext = alltext
      this.postForm.textnum = Math.ceil(this.realLength(alltext) / 2)
    },
    realLength(str) {
      let curlen = 0
      for (var i = 0; i < str.length; i++) {
        if (str.charCodeAt(i) > 255) {
          curlen += 2
        } else {
          curlen++
        }
      }
      return curlen
    },
    editInfo(flag) {
      this.isEdit = flag
      this.isShowPaper = true
      if (this.isEdit && this.paperFlag === '0') {
        this.isShowPaper = false
      }
      if (flag) {
        this.postForm.id = this.detail.id
        this.postForm.infoClass = this.detail.classId
        this.changeInfoClass()
        this.postForm.title = this.detail.title
        this.postForm.content = this.detail.content
        this.postForm.phone = this.detail.phone
        this.postForm.phone2 = this.detail.phone2
        this.postForm.textnum = this.detail.textnum
        this.postForm.textb = this.detail.textb
        this.postForm.weixin = !!this.detail.weixin
        this.postForm.wxTopDays = this.detail.wxTopDays
        this.postForm.vols = this.detail.vols
        this.postForm.startVol = this.detail.vols.split(',')[0]
        this.postForm.volnum = this.detail.volnum
        this.postForm.week1 = !!this.detail.week1
        this.postForm.week3 = !!this.detail.week3
        this.postForm.week5 = !!this.detail.week5
        this.postForm.wxNum = this.detail.wxNum
        this.checkedWeek = []
        if (this.detail.wxWeek1) {
          this.checkedWeek.push(1)
        }
        if (this.detail.wxWeek2) {
          this.checkedWeek.push(2)
        }
        if (this.detail.wxWeek3) {
          this.checkedWeek.push(3)
        }
        if (this.detail.wxWeek4) {
          this.checkedWeek.push(4)
        }
        if (this.detail.wxWeek5) {
          this.checkedWeek.push(5)
        }
        if (this.detail.wxWeek6) {
          this.checkedWeek.push(6)
        }
        if (this.detail.wxWeek7) {
          this.checkedWeek.push(7)
        }
        this.handleCheckedWeeksChange(this.checkedWeek)
        this.changeTextB()
      }
    },
    noPassInfo(id) {
      if (!this.postForm.delermarks) {
        this.$message({
          message: '请填写未通过原因',
          type: 'error'
        })
        return false
      }
      const para = { infoId: id, delermarks: this.postForm.delermarks }
      this.loading = true
      this.$http
        .post('/api/pay/refund', para)
        .then(response => {
          this.loading = false
          if (response.data.success === true) {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            this.previewVisible = false
            setTimeout(this.getList(), 2000)
            this.postForm.delermarks = ''
          } else {
            console.log('获取失败:' + response.data.message)
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
          this.loading = false
        })
    },
    passInfo(id) {
      const para = { id: id, status: 3 }
      this.loading = true
      this.$http
        .post('/api/busInfo/audit', para)
        .then(response => {
          this.loading = false
          if (response.data.success === true) {
            this.$message({
              message: '审核成功',
              type: 'success'
            })
            this.previewVisible = false
            this.getList()
          } else {
            console.log('获取失败:' + response.data.message)
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
          this.loading = false
        })
    },
    addVol(vol) {
      const year = new Date(vol.pubdate).getFullYear()
      const nowyear = new Date().getFullYear()
      let volname = parseInt(vol.name)
      if (year !== nowyear) {
        volname = (year - 2000) * 1000 + volname
      }
      this.listQuery.vol = volname
      this.listQuery.volid = vol.id
      this.calendarVisible = false
    },
    showVolName(date) {
      const item = this.monthVolList.find(item => item.pubdate === date)
      if (item) {
        const year = new Date(item.pubdate).getFullYear()
        let volname = parseInt(item.name)
        volname = (year - 2000) * 1000 + volname
        return volname
      }
    },
    showVolData(date) {
      const item = this.monthVolList.find(item => item.pubdate === date)
      if (item) {
        return item
      }
    },
    formatYear(str) {
      const year = parseInt('20' + str.toString().substring(0, 2))
      const nowYear = new Date().getFullYear()
      if (year === nowYear) {
        return parseInt(str.toString().substring(2, 5))
      } else {
        return str
      }
    },
    checkVols(vols) {
      let checkVolList = vols || this.postForm.vols.toString().replace(/，/g, ',').replace(/。/g, ',').replace(/\./g, ',').split(',')
      checkVolList = [...new Set(checkVolList)]
      // 此处有过去年份排序错误的问题，比如21001会排到100期后面
      checkVolList = checkVolList.sort(function(a, b) {
        return parseInt(a) - parseInt(b)
      })
      checkVolList = checkVolList.filter(item => item)
      const weekList = []
      if (this.week1) { weekList.push(1) }
      if (this.week3) { weekList.push(3) }
      if (this.week5) { weekList.push(5) }
      this.$http.post('/api/busVol/checkVolList', { volList: checkVolList, beforeDefault: this.$route.query.type === 2, weekList: weekList })
        .then(response => {
          if (response.data.result === true) {
            this.postForm.vols = checkVolList.join(',')

            this.selectVols = this.postForm.vols.split(',').map(item => {
              if (parseInt(item) < 1000) {
                return (new Date().getFullYear() - 2000) * 1000 + parseInt(item)
              } else {
                return parseInt(item)
              }
            })
            // const pubdate = new Date(response.data.result.pubdate)
            // const nowYear = new Date(response.data.result.pubdate).getFullYear()
            // if (pubdate.getFullYear() !== nowYear) {
            //   this.postForm.startVol = (pubdate.getFullYear() - 2000) * 1000 + parseInt(this.postForm.startVol)
            // }

            // console.log('this.postForm.startVol')
            // console.log(this.postForm.startVol)
          } else {
            this.postForm.vols = ''
            this.$message({
              message: '期数填写有误',
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
        })
    },
    showCalendar() {
      // this.checkVols()
      this.calendarVisible = true
      this.getVolsByDate()
    },
    getVolsByDate() {
      const myDate = this.myMonth || new Date()
      const month = myDate.getMonth() + 1
      const year = myDate.getFullYear()
      this.$http
        .get('/api/busVol/getVolByDate?year=' + year + '&month=' + month)
        .then(response => {
          if (response.data.success === true) {
            this.monthVolList = response.data.result
          } else {
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
        })
    },
    getDefaultVol() {
      this.$http
        .get('/api/busVol/getDefaultVol')
        .then(response => {
          if (response.data.success === true) {
            let defaultVol = response.data.result.name
            const pubdate = new Date(response.data.result.pubdate)
            const nowYear = new Date(response.data.result.pubdate).getFullYear()
            if (pubdate.getFullYear() !== nowYear) {
              defaultVol = (pubdate.getFullYear() - 2000) * 1000 + parseInt(defaultVol)
            }
            this.listQuery.vol = defaultVol
            this.listQuery.volid = response.data.result.id
          } else {
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
        })
    },
    addInfo(id) {
      this.$router.push({ name: 'addinfo', query: { id: id, type: 1 }})
    },
    detailShow(id) {
      this.isEdit = false
      this.isShowPaper = true
      this.previewVisible = true
      this.$http
        .get('/api/busInfo/getById?id=' + id)
        .then(response => {
          if (response.data.success === true) {
            this.detail = response.data.result
            const wxWeeksArr = []
            if (this.detail.wxWeek1) {
              wxWeeksArr.push('周一')
            }
            if (this.detail.wxWeek2) {
              wxWeeksArr.push('周二')
            }
            if (this.detail.wxWeek3) {
              wxWeeksArr.push('周三')
            }
            if (this.detail.wxWeek4) {
              wxWeeksArr.push('周四')
            }
            if (this.detail.wxWeek5) {
              wxWeeksArr.push('周五')
            }
            if (this.detail.wxWeek6) {
              wxWeeksArr.push('周六')
            }
            if (this.detail.wxWeek7) {
              wxWeeksArr.push('周日')
            }
            this.detail.wxWeeksText = wxWeeksArr.length !== 7 ? wxWeeksArr.join('、') : ''
            let alltext = this.detail.title + ':' + this.detail.content
            if (this.detail.phone) {
              alltext = alltext + '。电话：' + this.detail.phone
            }
            if (this.detail.phone2) {
              alltext = alltext + ' ' + this.detail.phone2
            }
            this.detail.alltext = alltext
            this.detail.vols = this.detail.volList.map(item => {
              const year = new Date(item.pubdate).getFullYear()
              const nowYear = new Date().getFullYear()
              if (year !== nowYear) {
                return (year - 2000) * 1000 + parseInt(item.name)
              } else {
                return item.name
              }
            }).join(',')
          } else {
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
        })
    },
    showTextB(val) {
      if (this.detail.classId === 70) {
        switch (val) {
          case 2:
            return '标下'
          case 3:
            return '标头'
        }
      } else {
        switch (val) {
          case 0:
            return '普通'
          case 1:
            return '加黑'
          case 2:
            return '提前'
          case 3:
            return '加黑+提前'
        }
      }
    },
    infoClassName(id) {
      let classname = ''
      this.infoClassList.forEach(item => {
        item.children.forEach(it => {
          if (it.id === id) {
            classname = item.classname + ' > ' + it.classname
          }
        })
      })
      return classname
    },
    getInfoClass() {
      this.$http
        .get('/api/busInfoClass/getLevelList')
        .then(response => {
          if (response.data.success === true) {
            this.infoClassList = response.data.result
          } else {
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
        })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    sortChange(column, prop, order) {
      this.Sortkey = column.prop
      if (column.order === 'ascending') {
        this.Isace = 'asc'
      } else {
        this.Isace = 'desc'
      }
      this.getList()
    },
    handleFilter() {
      this.btnLoading = true
      if (!this.listQuery.vol) {
        this.$message({
          message: '请填写期数',
          type: 'error'
        })
        return false
      }
      this.listQuery.page = 1
      this.getList()
    },
    handleWxFilter() {
      this.btnLoading = true
      this.listQuery.page = 1
      this.wxVolname = -1
      this.getList()
    },
    getList() {
      const para = {
        source: this.source,
        realname: this.listQuery.realname,
        phone: this.listQuery.phone,
        status: this.listQuery.status,
        id: this.listQuery.id,
        key: this.listQuery.key,
        idcard: this.listQuery.idcard,
        parentClassId: this.listQuery.infoClass,
        volname: this.wxVolname === -1 ? this.wxVolname : parseInt(this.listQuery.vol),
        orderField: this.Sortkey,
        orderType: this.Isace,
        pageNum: this.listQuery.page,
        pageSize: this.listQuery.limit,
        notSelfInput: this.source === 1 ? 1 : 0
        // notSelfPassed: this.listQuery.status === 2 ? 1 : 0
        // publishTime: this.listQuery.publishTime,
        // title: this.listQuery.title
      }
      this.listLoading = true
      this.$http
        .post('/api/busInfo/queryByPage', para)
        .then(response => {
          this.listLoading = false
          this.btnLoading = false
          if (response.data.success === true) {
            this.showList = true
            this.list = response.data.result.records
            this.total = response.data.result.total
          } else {
            console.log('获取失败:' + response.data.message)
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
          this.listLoading = false
          this.btnLoading = false
        })
      // fetchList(this.listQuery).then(response => {
      //   this.list = response.data.items
      //   this.total = response.data.total
      //   this.listLoading = false
      // })
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
<style>
.inputtable{
  background:#ccc;
  line-height: 150%;
}
</style>
